home *** CD-ROM | disk | FTP | other *** search
- Path: wv.mentorg.com!bloom
- From: bloom@wv.mentorg.com (Scott Aron Bloom)
- Newsgroups: comp.lang.c++
- Subject: Re: Overloading conversion
- Date: 23 Jan 1996 00:27:11 GMT
- Organization: Mentor Graphics, Wilsonville, Oregon, USA
- Distribution: world
- Message-ID: <4e1a0v$dvh@hpbab.wv>
- References: <4d934v$10hs@ds2.acs.ucalgary.ca>
- NNTP-Posting-Host: glock.wv
-
- In article <4d934v$10hs@ds2.acs.ucalgary.ca>, hdang@acs3.acs.ucalgary.ca (Hanna Dang) writes:
- |> Is there a way to overload functions such that certain
- |> functions are called depending on the context the object are
- |> used. For example,
- |>
- |> class T {
- |> public:
- |> int& int_value() { modified = TRUE; return value;}
- |> int int_value() {return value;}
- |> private:
- |> int value;
- |> int modified;
- |> }
- |>
- |>
- |> ...
- |>
- |> T a;
- |> int test = a + 6 // int int_value should be called;
- |> a = 20 // int& int_value should be called.
- |>
- |> However, this does not work because in both case int& int_value
- |> is called. Is there another way to find out if value is changed
- |> or it's just inspected.
- |>
-
-
- To tell you the truth, I am kinda shocked that class T compiled
- at all.
-
- My compiler gives the following error message:
-
- CC: "t.C", line 4: error: two different return value types for T::int_value(): int & and int (1102)
- CC: "t.C", line 4: error: two definitions of T::int_value() (1034)
- CC: "t.C", line 4: error: T::int_value() cannot be redeclared in class declaration (1168)
-
-
- And this is what i would expect. Remember, return values from functions in
- C/C++ are not required to be used. Therefor you can not overload in C++
- based off of the return type.
-
- What you really want is something like PASCAL has, the procedure/function
- difference.
-
- While I have tried to figure out ways to do what you want, I usually
- give up. The method I sometimes use is having a member function T::value().
-
- So for your code you would use
-
- T a;
- int test = a.value + 6;
- a = 20;
-
-
- Sorry to be the bearer of bad news...
-
- Scott
-
- --
- ----------------------------------------------------------
- Scott Aron Bloom email: bloom@wv.mentorg.com
- Mentor Graphics Corporation main: (503) 685-7000
- 8005 S.W. Boeckman Road C/4 direct: (503) 685-7996
- Wilsonville, OR 97070-7777 fax: (503) 685-1268
- =============== Silicon Systems Division ===============
- ----------------------------------------------------------
-